package com.qcby.mycommunity_003.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.mycommunity_003.entity.User;
import com.qcby.mycommunity_003.mapper.UserMapper;
import com.qcby.mycommunity_003.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.mycommunity_003.vo.PageVo;
import com.qcby.mycommunity_003.vo.UserListVo;
import com.qcby.mycommunity_003.vo.UserResultVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-03-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public PageVo userList(UserListVo userListVo) {
        Page<User> page=new Page();
        page.setCurrent(userListVo.getPage());
        page.setSize(userListVo.getLimit());
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(userListVo.getUsername()),"username",userListVo.getUsername());
        queryWrapper.like(StringUtils.isNotBlank(userListVo.getRealName()),"real_name",userListVo.getRealName());
        page = userMapper.selectPage(page, queryWrapper);
        PageVo Vo=new PageVo();
        Vo.setTotalCount(page.getTotal());
        Vo.setPageSize(page.getSize());
        Vo.setCurrPage(page.getCurrent());
        Vo.setTotalPage(page.getPages());
        ArrayList<UserResultVo> list=new ArrayList<>();
        for (User user : page.getRecords()) {
            UserResultVo u=new UserResultVo();
            BeanUtils.copyProperties(user,u);
            String rolename=userMapper.selectRolenme(user.getUserId());
            u.setRoleName(rolename);
            list.add(u);
        }
        Vo.setList(list);
        return Vo;
    }
}
